package cn.edu.xmu.wnw.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.xmu.wnw.javabean.UserService;
import cn.edu.xmu.wnw.pojo.User;


public class UserServiceServlet extends HttpServlet
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserService us = new UserService(request.getSession());
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		PrintWriter out = response.getWriter();
		if (us.createUser(user)){
			out.println("<script type=\"text/javascript\"><!-- window.alert(\"hello\");//--></script>");
			response.sendRedirect("info.jsp");
		}
		else{
			out.println("<script>window.alert(\"fail\");</script>");
			out.println("<script>javascript:window.history</script>");
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		doPost(req, resp);
	}
}
